package light.mvc.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.jifan.pssmis.foundation.log.SysLogger;

public class PropertiesUtil {
	public static Map<String, Properties> protertiesMap = new HashMap<String, Properties>();

	// static{
	// try {
	// // Properties appproperties = new Properties();
	// // File appfile = new
	// File(Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
	// + "app_config.properties");
	// // InputStream appin = new BufferedInputStream(new
	// FileInputStream(appfile));
	// // appproperties.load(appin);
	// // protertiesMap.put("app_config.properties", appproperties);
	// //
	// //
	// // Properties serverproperties = new Properties();
	// // File serverfile = new
	// File(Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
	// + "server_config.properties");
	// // InputStream serverin = new BufferedInputStream(new
	// FileInputStream(serverfile));
	// // serverproperties.load(serverin);
	// // protertiesMap.put("server_config.properties", serverproperties);
	// //
	// // Properties bcmproperties = new Properties();
	// // File bcmfile = new
	// File(Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()
	// + "bcm.properties");
	// // InputStream bcmin = new BufferedInputStream(new
	// FileInputStream(bcmfile));
	// // bcmproperties.load(bcmin);
	// // protertiesMap.put("bcm.properties", bcmproperties);
	//			
	//			
	// } catch (FileNotFoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (URISyntaxException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	public static String getValue(String fileName, String propertyName,
			String defalutValue) {
		try {
			Properties properties = getPropertyes(fileName);
			return properties.getProperty(propertyName, defalutValue);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return defalutValue;
	}

	public static void setValue(String fileName, String propertyName,
			String defalutValue) {

		try {
			Properties props = getPropertyes(fileName);
			OutputStream fos = new FileOutputStream(Thread.currentThread()
					.getContextClassLoader().getResource("").toURI().getPath()
					+ fileName);
			props.setProperty(propertyName, defalutValue);
			// 以适合使用 load 方法加载到 Properties 表中的格式，
			// 将此 Properties 表中的属性列表（键和元素对）写入输出流
			props.store(fos, "Update '" + propertyName + "' " + defalutValue);
			protertiesMap.put("fileName", props);

		} catch (Exception e) {
		}

	}

	public static String getValue(Properties properties, String propertyName,
			String defalutValue) {
		try {
			return properties.getProperty(propertyName, defalutValue);
		} catch (Exception e) {
		}
		return "";
	}

	public static Integer getIntValue(String fileName, String propertyName,
			String defalutValue) {
		try {
			Properties properties = getPropertyes(fileName);
			return Integer.valueOf(properties.getProperty(propertyName,
					defalutValue));
		} catch (Exception e) {
		}
		return 0;
	}

	public static Boolean getBooleanValue(String fileName, String propertyName,
			String defalutValue) {
		try {
			Properties properties = getPropertyes(fileName);
			return Boolean.valueOf(properties.getProperty(propertyName,
					defalutValue));
		} catch (Exception e) {
		}
		return false;
	}

	/**
	 * @param fileName
	 * @return
	 * @throws URISyntaxException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static Properties getPropertyes(String fileName)
			throws URISyntaxException, FileNotFoundException, IOException {
		if (protertiesMap.containsKey(fileName)) {
			return protertiesMap.get(fileName);
		} else {
			Properties properties = new Properties();
			File file=null;
			InputStream in=null;
			try {
				
				file = new File(Thread.currentThread()
						.getContextClassLoader().getResource("").toURI()
						.getPath()
						+ fileName);
				in = new BufferedInputStream(new FileInputStream(
						file));
				properties.load(in);
			} catch (Exception e) {
				SysLogger.error(PropertiesUtil.class, "getPropertyes异常！文件名"+fileName+","+e.getMessage());
			}finally{
				if(in!=null){
					in.close();
					in=null;
				}
			}
			return properties;
		}
	}
}
